//
//  CurtainsController.m
//  GBKHOME
//
//  Created by XMYY-19 on 2017/9/5.
#import "CurtainsController.h"
#import "Global1.h"
#import "Global1.h"
#import "LoginController1.h"
#import "eventAction.h"

@interface CurtainsController ()

- (IBAction)kaiguanBtnClick:(UIButton *)sender;
@property (retain, nonatomic) IBOutlet UIImageView *chuanlian;
@property (retain, nonatomic) IBOutlet UIButton *closeBtn;
@property (retain, nonatomic) IBOutlet UIButton *stopBtn;
@property (retain, nonatomic) IBOutlet UIButton *kaiguan;
@property (nonatomic,assign) BOOL ischangeBtnIndex;
@property (nonatomic, assign) BOOL pauseClickDown;

//是否开启
@property(nonatomic,assign)BOOL isClosed;

@end

@implementation CurtainsController
-(id)initWithEquipment:(NewEquipment *)equ
{
    self = [super init];
    if (self)
    {
        self.isClosed = ([equ.state integerValue]==1)?YES:NO;
        self.equipment = equ;
        self.pauseClickDown = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.view.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"mainbg"]];
    [self setNavigationTitleView:_equipment.name];
    UIImage *image = [UIImage imageNamed:@"mainbg"];
    self.view.layer.contents = (id) image.CGImage;
    
    [self initWithUI];
}

-(void)initWithUI
{
    CGFloat btnWidth = (SCREEN_WIDTH- 20*4)/3;
    self.kaiguan.backgroundColor =[UIColor colorWithRGBColorByString:@"#17174c"];
    [self.kaiguan setTitle:@"开" forState:UIControlStateNormal];
    self.kaiguan.layer.cornerRadius = 20;
    [self.kaiguan setTintColor:[UIColor colorWithRGBColorByString:@"#f8f9fa"]];
    self.kaiguan.frame = CGRectMake(20, 300, btnWidth, 40);

    self.closeBtn.backgroundColor =[UIColor colorWithRGBColorByString:@"#17174c"];
    [self.closeBtn setTitle:@"闭" forState:UIControlStateNormal];
    self.closeBtn.layer.cornerRadius = 20;
    [self.closeBtn addTarget:self action:@selector(kaiguanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.closeBtn setTintColor:[UIColor colorWithRGBColorByString:@"#66f8f9fa"]];
    self.closeBtn.frame = CGRectMake(20*2+btnWidth, self.kaiguan.top, self.kaiguan.width, 40);
    
    self.stopBtn.backgroundColor =[UIColor colorWithRGBColorByString:@"#17174c"];
    [self.stopBtn setTitle:@"停" forState:UIControlStateNormal];
    self.stopBtn.layer.cornerRadius = 20;
    [self.stopBtn addTarget:self action:@selector(stopClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.stopBtn setTintColor:[UIColor colorWithRGBColorByString:@"#66f8f9fa"]];
    self.stopBtn.frame = CGRectMake(20*3+btnWidth*2, self.kaiguan.top, self.kaiguan.width, 40);

}

- (void)dealloc {
    [_kaiguan release];
    [_stopBtn release];
    [_closeBtn release];
    [_chuanlian release];
    [super dealloc];
}

- (IBAction)kaiguanBtnClick:(UIButton *)sender
{
    BOOL isOpen;
    NSString *switchId = self.equipment.switchId;
    CGRect rect = self.chuanlian.frame;
    if (sender == _closeBtn) {
        self.isClosed = YES;
        isOpen = YES;
        //关窗帘动画
        if (self.pauseClickDown) {
            [self animationContinue];
        } else {
            [UIView animateWithDuration:5 animations:^{
                self.chuanlian.frame =CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 164);
            }];
        }
        self.kaiguan.backgroundColor = [UIColor clearColor];
        self.closeBtn.backgroundColor = [UIColor colorWithRGBColorByString:@"#23e5f2"];
        self.stopBtn.backgroundColor = [UIColor clearColor];
    }else if(sender == _kaiguan ){
        self.isClosed = NO;
        isOpen = NO;
        //关窗帘动画
        if (self.pauseClickDown) {
            [self animationContinue];
        } else {
            [UIView animateWithDuration:5 animations:^{
                self.chuanlian.frame =CGRectMake(rect.origin.x, rect.origin.y, rect.size.width,1);
            }];
        }
        self.kaiguan.backgroundColor = [UIColor colorWithRGBColorByString:@"#23e5f2"];
        self.closeBtn.backgroundColor = [UIColor clearColor];
        self.stopBtn.backgroundColor = [UIColor clearColor];
    }
    [eventAction electricCompantWithOrder:isOpen swtichId:switchId result:^(NSDictionary *dic) {
        [self ctlDeviceEnd:dic];
    }];
    self.pauseClickDown = NO;
}

- (void)stopClick:(UIButton *)sender{
    [self animationPause];
    NSString *switchId = self.equipment.switchId;
    [eventAction electricCurtainsWithOrder:YES swtichId:switchId result:^(NSDictionary *dic) {
        ;
    }];
    self.pauseClickDown = YES;
    self.kaiguan.backgroundColor = [UIColor clearColor];
    self.closeBtn.backgroundColor = [UIColor clearColor];
    self.stopBtn.backgroundColor = [UIColor colorWithRGBColorByString:@"#23e5f2"];
}

- (void)animationPause{
    // 将当前时间CACurrentMediaTime转换为layer上的时间, 即将parent time转换为local time
    CFTimeInterval pauseTime = [self.chuanlian.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    
    // 设置layer的timeOffset, 在继续操作也会使用到
    self.chuanlian.layer.timeOffset = pauseTime;
    
    // local time与parent time的比例为0, 意味着local time暂停了
    self.chuanlian.layer.speed = 0;
}

- (void)animationContinue {
    // 时间转换
    CFTimeInterval pauseTime = self.chuanlian.layer.timeOffset;
    // 计算暂停时间
    CFTimeInterval timeSincePause = CACurrentMediaTime() - pauseTime;
    // 取消
    self.chuanlian.layer.timeOffset = 0;
    // local time相对于parent time世界的beginTime
    self.chuanlian.layer.beginTime = timeSincePause;
    // 继续
    self.chuanlian.layer.speed = 1;
}

- (void)ctlDeviceEnd:(NSDictionary *)dic
{
    if (self.equipment == nil) {//后台刷新电器状态，更新状态，已重载电器，此电器已释放
        NSLog(@"nil");
        return;
    }
    NSString *tip = @"";
    BOOL OPEN = [DeviceView deviceStateOn:self.equipment];
    BOOL STATE = !OPEN;
    NSNumber *result = [dic objectForKey:@"result"];
    if (result.integerValue == 0) {
        
        tip = [OperationTip operat:self.equipment.name open:STATE result:YES errorType:ErrorTypeServerReturnFail];
        [self setDeviceOn:STATE];
        
        GKBMainController *main = [GKBMainController mainCtr];
        [main updateStateByNet];
    }
    else
    {
        tip = [OperationTip operat:self.equipment.name open:STATE result:NO errorType:ErrorTypeServerReturnFail];
    }
    
    [iToast showTip:tip gravity:iToastGravityBottom];
}

- (void)setDeviceOn:(BOOL)on
{
    //    self.Switch = on;
    //    self.button.selected = on;
    if (on) {
        //        [self.button setBackgroundImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal];
        self.equipment.state = kStateON;
    }
    else
    {
        //        [self.button setBackgroundImage:[UIImage imageNamed:@"lightBlue.png"] forState:UIControlStateNormal];
        self.equipment.state = kStateOFF;
    }
    
    
    // [self updateProtectName];
    
}

+ (void)ctlDeviceSwithcId:(NSString *)swId name:(NSString *)name OnOff:(BOOL)open target:(id)target end:(SEL)action
{
    NSString *order = @"";
    NSString *switchId = swId;
    if (open) {
        order = @"OPEN";
    }
    else
    {
        order = @"CLOSE";
    }
    
    NSString *tipStart = [OperationTip startOperat:open name:name];
    [iToast showTip:tipStart gravity:iToastGravityBottom];
    
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    
    [WebServices setShowConnectting:NO];
    
    NSString *errorTip = [OperationTip operat:name open:open result:NO errorType:ErrorTypeNetworkFail];
    
    [WebServices requestStartOrder:@"ctlDevice" body:body target:target action:action errorTip:errorTip];
}

@end
